package com.dragee.modules.officialWebsite.controller;

import cn.hutool.core.util.StrUtil;
import com.dragee.common.PageUtils;
import com.dragee.common.R;
import com.dragee.config.exception.BusinessErrorType;
import com.dragee.config.exception.BusinessException;
import com.dragee.constants.CommonConstants;
import com.dragee.modules.officialWebsite.entity.WebSiteNoticeEntity;
import com.dragee.modules.officialWebsite.service.WebSiteNoticeService;
import com.dragee.modules.sys.entity.FilesEntity;
import com.dragee.modules.sys.service.FilesService;
import com.dragee.utils.UploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Map;



/**
 * 新闻
 *
 * @author tc
 * @email tc@mail.com
 * @date 2023-04-27 13:33:26
 */
@RestController
@RequestMapping("officialWebsite/notice")
@Api(tags = "officialWebsite - 公告")
public class WebSiteNoticeController {
    @Autowired
    private WebSiteNoticeService webSiteNoticeService;
    @Autowired
    private FilesService filesService;
    private final String fileFlag = "officialWebsite_notice";
    @Value("${webull.mapUploadPath}")
    private String rootPath;


    @PostMapping("upload")
    @ApiOperation("上传文件")
    public R upload(MultipartFile file) {
        if (file == null) {
            throw new BusinessException(BusinessErrorType.INVALID_PARAMS);
        }
        String path = rootPath + CommonConstants.PIC_DIR + "/" + LocalDateTime.now().format(DateTimeFormatter.BASIC_ISO_DATE);
        String fileId = UploadUtil.uploadOne(file, path, fileFlag, null);
        return R.ok().put("data", fileId);
    }

    /**
     * 列表
     */
    @GetMapping("list")
    @ApiOperation("新闻列表")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = webSiteNoticeService.queryPage(params);
        return R.ok().put("data", page);
    }


    /**
     * 信息
     */
    @GetMapping("{id}")
    @ApiOperation("新闻信息")
    public R info(@PathVariable("id") Long id){
		WebSiteNoticeEntity notice = webSiteNoticeService.getById(id);
        FilesEntity files = UploadUtil.getFilesEntity(notice.getFileId());
        if (files != null) {
            notice.setFileName(files.getFileName());
            notice.setFilePath(files.getFilePath());
        }
        return R.ok().put("data", notice);
    }

    /**
     * 保存
     */
    @PostMapping("saveOrUpdate")
    @ApiOperation("保存或修改新闻")
    public R saveOrUpdate(@RequestBody WebSiteNoticeEntity notice){
        webSiteNoticeService.saveOrUpdate(notice);
        return R.ok();
    }


    /**
     * 删除
     */
    @GetMapping("delete-by-id")
    @ApiOperation("新闻删除")
    public R delete( String id){
        WebSiteNoticeEntity o = webSiteNoticeService.getById(id);
        if (StrUtil.isNotBlank(o.getFileId())) {
            filesService.removeById(o.getFileId());
        }
        webSiteNoticeService.removeById(Long.parseLong(id));
        return R.ok();
    }

    @GetMapping("delete-by-ids")
    @ApiOperation("新闻批量删除")
    public R deletes(String ids){
        String[] idArr = ids.split(",");
        Arrays.stream(idArr).forEach(id -> webSiteNoticeService.removeById(Long.parseLong(id)));
        return R.ok();
    }

}
